//
//  CircleSliderView.m
//  MicrowaveLight
//
//  Created by Kendama on 2017/6/16.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "CircleSliderView.h"
#import "JXCircleSlider.h"
#import "UIView+ColorOfPoint.h"

@interface CircleSliderView ()

@property(nonatomic,strong) JXCircleSlider *slider;
@property(nonatomic,strong) UIImageView *imageView;
@property(nonatomic,strong) UIView *colorBackgroundView;
@property(nonatomic,strong) UIImageView *lightImageView;

@end

@implementation CircleSliderView

- (instancetype)init{
    
    if ([super init]) {
        self.backgroundColor = [UIColor whiteColor];
        [self masLayoutSubview];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        [self masLayoutSubview];
    }
    return self;
}


#pragma mark -- 回调
-(void)newValue:(JXCircleSlider*)slider{
    //    NSLog(@"newValue:%d",slider.angle);
    //    NSLog(@"point:x=%f,y=%f",slider.point.x,slider.point.y);
    self.slider.pointColor = [self.imageView colorOfPoint:slider.point];
    self.colorBackgroundView.backgroundColor = [self.imageView colorOfPoint:slider.point];
    self.colorBackgroundView.layer.shadowColor = [self.imageView colorOfPoint:slider.point].CGColor;
    self.colorBackgroundView.layer.shadowOffset = CGSizeMake(0, 0);
    self.colorBackgroundView.layer.shadowOpacity = 0.5;
    self.colorBackgroundView.layer.shadowRadius = 10.0;
    
    if (self.returnColorBlock) {
        self.returnColorBlock([self.imageView colorOfPoint:slider.point]);
    }
}

-(void)endValue:(JXCircleSlider*)slider{
    if (self.returnColorBlock) {
        self.returnColorBlock([self.imageView colorOfPoint:slider.point]);
    }
}


- (void)masLayoutSubview {
    __weak UIView *superView = self;
    CGFloat edge = 10;
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView).with.offset(edge);
        make.left.mas_equalTo(superView).with.offset(edge);
        make.right.mas_equalTo(superView).with.offset(-edge);
        make.bottom.mas_equalTo(superView).with.offset(-edge);
    }];


    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superView);
    }];
    
    [self.colorBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.imageView);
        make.height.width.mas_equalTo(px1080Width(460));
    }];
    
    
    
    [self.lightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.imageView);
        make.height.width.mas_equalTo(px1080Width(270));
    }];
}

#pragma mark -- 懒加载
- (UIImageView *)imageView
{
    if(!_imageView)
    {
        __weak UIView *superView = self;
        _imageView = [[UIImageView alloc]init];
        [superView addSubview:_imageView];
//        _imageView.contentMode = UIViewContentModeScaleAspectFit;

//        _imageView.image = [UIImage imageNamed:self.imageName];
    }
    return _imageView;
}

- (JXCircleSlider *)slider
{
    if(!_slider)
    {
        __weak UIView *superView = self;
        _slider = [[JXCircleSlider alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.width)];
        [superView addSubview:_slider];
        _slider.pointColor = [UIColor redColor];
        [_slider addTarget:self action:@selector(newValue:) forControlEvents:UIControlEventValueChanged];
        [_slider addTarget:self action:@selector(endValue:) forControlEvents:UIControlEventEditingDidEnd];
    }
    return _slider;
}



- (void)setImageColor:(UIColor *)imageColor {
    self.colorBackgroundView.backgroundColor = imageColor;
    self.slider.pointColor = imageColor;
}

- (void)setAngle:(int)angle {
    self.slider.angle = angle;
}

- (void)setImageName:(NSString *)imageName {
    self.imageView.image = [UIImage imageNamed:imageName];
}

- (UIView *)colorBackgroundView
{
    if(!_colorBackgroundView)
    {
        UIView *superView = self;
        _colorBackgroundView = [[UIView alloc]init];
        [superView addSubview:_colorBackgroundView];
        _colorBackgroundView.layer.cornerRadius = px1080Width(460)/2;
        
    }
    return _colorBackgroundView;
}

- (UIImageView *)lightImageView
{
    if(!_lightImageView)
    {
        UIView *superView = self.colorBackgroundView;
        _lightImageView = [[UIImageView alloc]init];
        [superView addSubview:_lightImageView];
        _lightImageView.image = [UIImage imageNamed:@"bulb"];
        _lightImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _lightImageView;
}

@end
